//
// Created by yonghu0 on 25-10-9.
//

#include <iostream>
#include <unordered_map> //unordered_multimap所在头文件
#include <functional> //greater的头文件
#include <utility> //pair的头文件

void print(std::unordered_multimap<int, int> &my_unordered_multimap)
{
    for (auto &elem: my_unordered_multimap)
    {
        std::cout << elem.first << " " << elem.second << std::endl;
    }
}


int main()
{
    std::cout << "unordered_multimap_test begin" << std::endl;

    std::cout << "--------------创建unordered_multimap----------------" << std::endl;
    std::unordered_multimap<int, int> my_unordered_multimap1;

    std::cout << "----------------容器size--------------" << std::endl;
    std::cout << my_unordered_multimap1.size() << std::endl;
    std::cout << "----------------容器判空---------------" << std::endl;
    std::cout << my_unordered_multimap1.empty() << std::endl;

    std::cout << "----------插入元素--------------------" << std::endl;
    //插入单个元素
    //不能用下标操作，例如my_unordered_multimap1[1]=11;
    //因为unordered_multimap有重复元素，my_unordered_multimap1[1]不知指哪个元素
    my_unordered_multimap1.insert(std::pair<int, int>(1, 11));
    my_unordered_multimap1.insert(std::pair<int, int>(2, 22));
    my_unordered_multimap1.insert(std::pair<int, int>(3, 33));
    my_unordered_multimap1.insert(std::pair<int, int>(4, 44));
    my_unordered_multimap1.insert(std::pair<int, int>(5, 55));
    print(my_unordered_multimap1);
    my_unordered_multimap1.insert(std::pair<int, int>(5, 555)); //插入重复元素
    print(my_unordered_multimap1);
    //插入区间

    std::cout << "------------查找元素(判断元素是否在容器中)-----------" << std::endl;
    //方法一
    if (my_unordered_multimap1.find(5) != my_unordered_multimap1.end())
    {
        std::cout << "元素在unordered_multimap中" << std::endl;
    } else
    {
        std::cout << "元素不在unordered_multimap中" << std::endl;
    }
    //方法二
    if (my_unordered_multimap1.count(5))
    {
        std::cout << "元素在unordered_multimap中" << std::endl;
    } else
    {
        std::cout << "元素不在unordered_multimap中" << std::endl;
    }

    std::cout << "------------修改unordered_multimap中的元素--------------" << std::endl;
    //通过迭代器修改元素的value
    //修改所有key为5的值，其value为99
    auto f_pair = my_unordered_multimap1.equal_range(5);
    for (auto it = f_pair.first; it != f_pair.second; it++)
    {
        it->second = 99;
    }
    print(my_unordered_multimap1);
    std::cout << "------------删除元素-----------------------------" << std::endl;
    //删除单个元素
    //方法一
    my_unordered_multimap1.erase(5);
    print(my_unordered_multimap1);
    my_unordered_multimap1.erase(11); //删除不存在的元素，不报错
    print(my_unordered_multimap1);
    //方法二
    my_unordered_multimap1.erase(my_unordered_multimap1.begin());
    print(my_unordered_multimap1);
    //删除区间
    //用的不多，这里就不列举

    std::cout << "----其他用法(equal_range)-----" << std::endl;
    //equal_range方法
    std::pair<std::unordered_multimap<int, int>::iterator, std::unordered_multimap<int, int>::iterator> p_equal_range;
    p_equal_range = my_unordered_multimap1.equal_range(3);
    std::cout << (p_equal_range.first)->first << " " << (p_equal_range.first)->second << std::endl;
    std::cout << (p_equal_range.second)->first << " " << (p_equal_range.second)->second << std::endl;

    std::cout << "------------清空容器-----------------" << std::endl;
    my_unordered_multimap1.clear();
    print(my_unordered_multimap1);

    std::cout << "unordered_multimap_test end" << std::endl;
    return 0;
}
